package com.eightylegs.portal;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * This is the class that is used during post processing.
 * 
 */
public class PostProcess {

	/**
	 * The output file format is a txt file.
	 */
	public static String FILE_EXTENSION = "txt";

	public PostProcess() {
	}

	/**
	 * This method is used for the first records during post processing. The new
	 * api use this. However, the old api use the postProcess method that have 4
	 * parameters. So both method should be available.
	 * 
	 * @param count
	 * @param url
	 * @param eightyFileData
	 * @param out
	 * @param fileNameWithoutExtension
	 * @return
	 * @throws Exception
	 */
	public static boolean postProcess(int count, String url, byte eightyFileData[], ZipOutputStream out, String fileNameWithoutExtension)
			throws Exception {

		// Creates the entry in the zip file. Depending on the type of output
		// file. If the output file is not txt, change the FILE_EXTENSION above.
		if (count == 0) {
			if (fileNameWithoutExtension != null) {
				out.putNextEntry(new ZipEntry((new StringBuilder(String.valueOf(fileNameWithoutExtension))).append(".").append(FILE_EXTENSION)
						.toString()));
			} else {
				out.putNextEntry(new ZipEntry("ProcessedResult." + FILE_EXTENSION));
			}
		}
		return process(count, url, eightyFileData, out);
	}

	/**
	 * This method is used for the first records during post processing. This
	 * method is ussed to create the zip entry for the first record.
	 * 
	 * @param count
	 * @param url
	 * @param eightyFileData
	 * @param out
	 * @param fileNameWithoutExtension
	 * @return
	 * @throws Exception
	 */
	public static boolean postProcess(int count, String url, byte eightyFileData[], ZipOutputStream out) throws Exception {
		if (count == 0) {
			out.putNextEntry(new ZipEntry("ProcessedResult." + FILE_EXTENSION));
		}
		return process(count, url, eightyFileData, out);
	}

	/**
	 * This method is called after all the records in the .80 file are
	 * processed.
	 * 
	 * @param count
	 * @param out
	 * @return
	 * @throws Exception
	 */
	public static boolean finalize(int count, ZipOutputStream out) throws Exception {
		return true;
	}

	/**
	 * This is the main method that gets executed.
	 * 
	 * @param count
	 *            The record number from all the files being processed.
	 * @param url
	 *            The url of the page that was analyzed.
	 * @param eightyFileData
	 *            The actual data returned by the 80app after the page is
	 *            analyzed
	 * @param out
	 *            Zip file stream which is used to write the processed data.
	 * @return
	 * @throws Exception
	 */
	private static boolean process(int count, String url, byte eightyFileData[], ZipOutputStream out) throws Exception {

		/*
		 * This takes the analysis data and outputs it to the file and put a new
		 * line after it. It does not output the url that was analyzed. If url
		 * needs to be added, take at the example 2.
		 */
		out.write(eightyFileData);
		out.write("\r\n".getBytes());

		/*
		 * Example 1: The following creates a new entry for each of the url. url
		 * = url.replace("http://", "").replace("/", "_").replace("\\", "_");
		 * out.putNextEntry(new ZipEntry(url)); out.write(eightyFileData);
		 */

		/*
		 * Example 2: out.write(url.getBytes()); out.write("\r\n".getBytes());
		 * out.write(eightyFileData); out.write("\r\n\r\n".getBytes());
		 */

		/*
		 * If the data is in xml format, need to escape url otherwise, the xml
		 * data will parsed correctly. url = url.replaceAll("\\$", "\\\\\\$");
		 */
		return false;
	}
}
